package cn.xo68.boot.auth.server.mapper;

import cn.xo68.boot.auth.server.domain.OauthResource;
import org.apache.ibatis.annotations.*;

import java.util.List;


/**
 * OauthResource mybatis mapper
 * @author wuyy(16349023@qq.com)
 * @date 2018年08月28日 14:33:41
 *
 */
public interface OauthResourceMapper{


    @Select("SELECT * FROM oauth_resource WHERE  client_id=#{clientId} and resource_id = #{resourceId}")
    @Results(value = {
            @Result(id = true, column = "resource_id", property = "resourceId"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "client_id", property = "clientId"),
            @Result(column = "resource_name", property = "resourceName"),
            @Result(column = "url", property = "url"),
            @Result(column = "permission", property = "permission"),
            @Result(column = "order_value", property = "orderValue"),
            @Result(column = "resource_type", property = "resourceType"),
            @Result(column = "operator_name", property = "operatorName"),
            @Result(column = "operator_id", property = "operatorId"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    OauthResource get(@Param("clientId")String clientId,@Param("resourceId") String resourceId);

    @Select("<script>SELECT * FROM oauth_resource WHERE  client_id=#{clientId} and <choose><when test=\"parentId !=null \">parent_id = #{parentId}</when ><otherwise>parent_id is null</otherwise></choose>  order by resource_id desc limit 1</script>")
    @Results(value = {
            @Result(id = true, column = "resource_id", property = "resourceId"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "client_id", property = "clientId"),
            @Result(column = "resource_name", property = "resourceName"),
            @Result(column = "url", property = "url"),
            @Result(column = "permission", property = "permission"),
            @Result(column = "order_value", property = "orderValue"),
            @Result(column = "resource_type", property = "resourceType"),
            @Result(column = "operator_name", property = "operatorName"),
            @Result(column = "operator_id", property = "operatorId"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    OauthResource getMaxChild(@Param("clientId")String clientId,@Param("parentId") String parentId);

    @Select("<script>SELECT * FROM oauth_resource WHERE  client_id=#{clientId} and <choose><when test=\"parentId !=null \">parent_id = #{parentId}</when ><otherwise>parent_id is null</otherwise></choose> order by resource_id asc</script>")
    @Results(value = {
            @Result(id = true, column = "resource_id", property = "resourceId"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "client_id", property = "clientId"),
            @Result(column = "resource_name", property = "resourceName"),
            @Result(column = "url", property = "url"),
            @Result(column = "permission", property = "permission"),
            @Result(column = "order_value", property = "orderValue"),
            @Result(column = "resource_type", property = "resourceType"),
            @Result(column = "operator_name", property = "operatorName"),
            @Result(column = "operator_id", property = "operatorId"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    List<OauthResource> listChild(@Param("clientId")String clientId,@Param("parentId") String parentId);


    @Select("SELECT * FROM oauth_resource WHERE  client_id=#{clientId} and resource_id = #{resourceId}")
    @Results(value = {
            @Result(id = true, column = "resource_id", property = "resourceId"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "client_id", property = "clientId"),
            @Result(column = "resource_name", property = "resourceName"),
            @Result(column = "url", property = "url"),
            @Result(column = "permission", property = "permission"),
            @Result(column = "order_value", property = "orderValue"),
            @Result(column = "resource_type", property = "resourceType"),
            @Result(column = "operator_name", property = "operatorName"),
            @Result(column = "operator_id", property = "operatorId"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    OauthResource find(@Param("clientId")String clientId,@Param("resourceId") String resourceId);

    @Select("SELECT * FROM oauth_resource where client_id=#{clientId} order by order_value,resource_id")
    @Results(value = {
            @Result(id = true, column = "resource_id", property = "resourceId"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "client_id", property = "clientId"),
            @Result(column = "resource_name", property = "resourceName"),
            @Result(column = "url", property = "url"),
            @Result(column = "permission", property = "permission"),
            @Result(column = "order_value", property = "orderValue"),
            @Result(column = "resource_type", property = "resourceType"),
            @Result(column = "operator_name", property = "operatorName"),
            @Result(column = "operator_id", property = "operatorId"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    List<OauthResource> list(@Param("clientId")String clientId);

    @Insert("insert into oauth_resource (resource_id ,parent_id ,client_id ,resource_name ,url ,permission ,order_value ,resource_type ,operator_name ,operator_id ,create_time ,update_time ) values (#{resourceId} , #{parentId} , #{clientId} , #{resourceName} , #{url} , #{permission} , #{orderValue} , #{resourceType} , #{operatorName} , #{operatorId} , #{createTime} , #{updateTime}  )")
    @Options(useGeneratedKeys = true, keyProperty = "resourceId")
    void insert(OauthResource oauthResource);

    @Update("update oauth_resource set  parent_id=#{parentId}, client_id=#{clientId}, resource_name=#{resourceName}, url=#{url}, permission=#{permission}, order_value=#{orderValue}, resource_type=#{resourceType}, operator_name=#{operatorName}, operator_id=#{operatorId}, update_time=#{updateTime} where client_id=#{clientId} and resource_id = #{resourceId}")
    void update(OauthResource oauthResource);

    @Delete("delete FROM oauth_resource WHERE client_id=#{clientId} and resource_id LIKE CONCAT(#{resourceId},'%')")
    void delete(@Param("clientId")String clientId, @Param("resourceId") String resourceId);
}